﻿using System.IO;
using System.Linq;
using System.Reflection;

namespace NFox.Pycad
{
    public static class DirectoryEx
    {

        public static FileInfo Location
        {
            get { return new FileInfo(Assembly.GetCallingAssembly().Location); }
        }

        public static DirectoryInfo CurrDirectory
        {
            get { return Location.Directory; }
        }

        public static DirectoryInfo GetDirectory(Assembly assem)
        {
            return new FileInfo(assem.Location).Directory;
        }

        public static DirectoryInfo GetDirectory(this DirectoryInfo dir, string name)
        {
            var dirs = dir.GetDirectories(name);
            if (dirs.Length > 0)
                return dirs[0];
            return null;
        }

        public static DirectoryInfo GetDirectory(this DirectoryInfo dir, params string[] names)
        {
            foreach (var name in names)
                dir = GetDirectory(dir, name);
            return dir;
        }

        public static DirectoryInfo GetDirectory(string name)
        {
            return GetDirectory(CurrDirectory, name);
        }

        public static DirectoryInfo GetDirectory(params string[] names)
        {
            return GetDirectory(CurrDirectory, names);
        }

        public static FileInfo GetFile(this DirectoryInfo dir, string name)
        {
            var files = dir.GetFiles(name);
            if (files.Length > 0)
                return files[0];
            return null;
        }

        public static FileInfo GetFile(string name)
        {
            return GetFile(CurrDirectory, name);
        }

        public static string GetFileFullName(this DirectoryInfo dir, string name)
        {
            string path = dir.FullName;
            if (path.Last() != '\\')
                path += "\\";
            return path + name;
        }

    }
}
